<?php
namespace App\Models;

use App\Models\Simple\XMLModel;
use App\Models\Simple\DatabaseModel;
/*
 * Mock travel destination data.
 * Note that we don't have to extend CodeIgniter's model for now
 */

// class Players extends XMLModel
// {
//     protected $origin = WRITEPATH . 'data/playersdata.xml';
//     protected $keyField = 'id';
//     protected $validationRules = [];
//     public function update($id, array $data)
//     {
//         // Check if the record exists
//         if (!isset($this->data[$id])) {
//             return false;
//         }

//         // Update each field with new data
//         foreach ($data as $field => $value) {
//             if (property_exists($this->data[$id], $field)) {
//                 $this->data[$id]->$field = (string)$value;
//             }
//         }

//         // Save changes back to the XML file
//         return $this->store();
//     }

//     /**


//      * Load the collection state from an XML document.
//      */
//     protected function load()
//     {
//         parent::load();
//     }

//     /**
//      * Store the collection state as an XML document.
//      */
//     protected function store()
//     {
//         return parent::store();
//     }
// }
class Players extends DatabaseModel
{
    protected $allowedFields = [
        'name',
        'description',
        'link',
        'image',
        'city',
        'powers',
        'weapon',
    ];
    /**
     * Constructor.
     */
    public function __construct()
    {
        // Assuming 'players' is the name of the database table
        parent::__construct('heroes', 'id');
    }

    /**
     * Update an existing record in the collection and the database.
     *
     * @param int   $id   The ID of the record to update.
     * @param array $data An associative array of field names and their new values.
     * @return bool True on success, false on failure.
     */
    public function update($id, array $data)
    {
        // Check if the record exists
        if (!isset($this->data[$id])) {
            return false;
        }

        // Update each field with new data
        foreach ($data as $field => $value) {
            if (property_exists($this->data[$id], $field)) {
                $this->data[$id]->$field = (string)$value;
            }
        }

        // Save changes back to the database
        return $this->store1($id);
    }
    // public function insert(array $data)
    // {
    //     // Start a transaction
    //     $this->db->transStart();

    //     // Use the inherited builder to insert the record
    //     if ($this->builder->insert($data)) {
    //         $insertID = $this->db->insertID();
    //     } else {
    //         $insertID = false;
    //     }

    //     // End the transaction
    //     $this->db->transComplete();

    //     if ($this->db->transStatus() === FALSE) {
    //         // If transaction failed, return false
    //         return false;
    //     }

    //     // Return the ID of the inserted record
    //     return $insertID;
    // }
    /**
     * Load the collection state from the database.
     */
    protected function load()
    {
        parent::load();
    }
    
    public function insert(array $data)
    {
        // Start a transaction
        $this->db->transStart();

        // Use the inherited builder to insert the record
        if ($this->db->table($this->table)->insert($data)) {
            $insertID = $this->db->insertID();
        } else {
            $insertID = false;
        }

        // End the transaction
        $this->db->transComplete();

        if ($this->db->transStatus() === FALSE) {
            // If transaction failed, return false
            return false;
        }

        // Return the ID of the inserted record
        return $insertID;
    }
    /**
     * Store the collection state into the database.
     */
    protected function store()
    {
        return parent::store();
    }
    protected function store1($id)
    {
        return parent::store1($id);
    }
}
